#include <opencv2/opencv.hpp>
#include <iostream>
#include<vector>

using namespace cv;
using namespace std;

int main()
{
    // 读取图像
    Mat image = imread("/home/lwj/opencv.21/2.png", IMREAD_GRAYSCALE);

    // 边缘检测
    Mat edges;
    Canny(image, edges, 50, 150, 3);

    // 应用Hough变换来检测直线
    vector<Vec4i> lines;
    HoughLinesP(edges, lines, 1, CV_PI / 180, 130,50,50);

    // 在图像上绘制检测到的线段
    Mat result;
    cvtColor(edges, result, COLOR_GRAY2BGR);//灰度
    for (size_t i = 0; i < lines.size(); i++)
    {
        Vec4i Line = lines[i];
        line(result, Point(Line[0], Line[1]), Point(Line[2], Line[3]), 
        Scalar(0, 0, 255), 3, LINE_AA);
    }

    // 显示原始图像和检测到的线段
    imshow("Original Image", image);
    imshow("Detected Lines", result);
    waitKey(0);

    return 0;
}